<?php
//ini_set('display_errors', 'On');
require_once 'base.php';
checkAuth();

$op = Utility::getParam('op', 'manage');
switch($op) {
	case 'add':
		$op();
		break;
	case 'doAdd':
		$op();
		break;
	case 'getDetail':
		$op();
		break;
	case 'getChild':
		$op();
		break;
	case 'modify':
		$op();
		break;
	case 'doModify':
		$op();
		break;
	case 'doDelete':
		$op();
		break;
	case 'doDeleteChild':
		$op();
		break;
	case 'manage':
		$op();
		break;
	default:
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		break;
}

function add() {
	
	$customerList = ObjectFinder::get('CustomerIndex')->getList(array('pid'=>0));

	include $GLOBALS['ROOT_PATH'] . '/views/admin/customer_add.phtml';
}

function doAdd() {
	
//		'pid':getSelectValue('nameSelect'),
//		'name':getSelectText('nameSelect'),
//		'childName':childName,
//		'area':province+'_'+city+'_'+area,
//		'address':address,
//		'zipcode':zipcode,
//		'contact':contact,
//		'tel':tel,
//		'email':email,

	$pid = max(-1, (int)Utility::getParam('pid', 0));
	$name = Utility::getParam('name', '');
	$childName = Utility::getParam('childName', '');
	$area = Utility::getParam('area', '');
	$address = Utility::getParam('address', '');
	$zipcode = Utility::getParam('zipcode', '');
	$contact = Utility::getParam('contact', '');
	$tel = Utility::getParam('tel', '');
	$email = Utility::getParam('email', '');
	
	//pid为-1，新建大类
	if($pid == -1 && empty($name)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;
	}
	
	if (empty($name) || empty($childName) || empty($area) || empty($address) 
		|| empty($zipcode) || empty($contact) || empty($tel)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;
	}
	
	$indexModel = ObjectFinder::get('CustomerIndex');
	//创建客户索引
	if ($pid == -1) {
		$customerIndexInfo = $indexModel->getInfoByName($name, 0);
		if (empty($customerIndexInfo)) {
			$pid = $indexModel->add(array('pid'=>$pid, 'name'=>$name));
		}else {
			$pid = (int)$customerIndexInfo['id'];
		}
	}
	if ($pid > 0) {
		$customerIndexInfo = $indexModel->getInfoByName($childName, $pid);
		if (empty($customerIndexInfo)) {
			$indexID = $indexModel->add(array('pid'=>$pid, 'name'=>$childName));
		}else {
			$indexID = (int)$customerIndexInfo['id'];
		}
	}
	
	//索引创建失败
	if ($pid <=0 || $indexID <= 0) {
		echoAjax(Constants::$ERR_FAILED);
		return ;	
	}
	
	//'customer_index', 'customer_index_pid', 'name', 'area', 'address', 'zip_code', 'contact', 'tel', 'email'
	$customerID = ObjectFinder::get('Customer')->add(array('customer_index' => $indexID, 'customer_index_pid' => $pid, 'name' => $name."(".$childName .")",
	 'area' => $area, 'address' => $address, 'zip_code' => $zipcode, 'contact' => $contact, 'tel' => $tel, 'email' => $email));
	
	$res = true;
	if ($customerID > 0) {
		$res = $indexModel->modify(array('customer_id' => $customerID), array('id' => $indexID));
	}
	echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function getDetail() {
	
	$idx = max(-1, (int)Utility::getParam('idx', 0));
	
	if (empty($idx) || $idx < 0) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	
	$info = ObjectFinder::get('Customer')->getInfoByIndex($idx);
	if (empty($info)) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	$info = $info[0];
	$data = array('id'=>$info['id'], 'area'=>$info['area'], 'address'=>$info['address'], 'zipcode'=>$info['zip_code'], 
				'contact'=>$info['contact'], 'tel'=>$info['tel'], 'email'=>$info['email']);
	echoAjax(array('errno'=>0, 'errmsg'=>'', 'data'=>$data));
}

function getChild() {
	
	$id = max(0, Utility::getParam('id', 0));
	if (empty($id)) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	
	$childList = ObjectFinder::get('CustomerIndex')->getList(array('pid'=>$id), 0, 100);
	$childList = $childList['data'];
	if (empty($childList)) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	
	$data = array();
	foreach($childList as $child) {
		$data[] = array('customerID'=>$child['customer_id'], 'index'=>$child['id'], 'name'=>$child['name']);
	}
	echoAjax(array('errno'=>0, 'errmsg'=>'', 'data'=>$data));
}

function modify() {
	
	$id = max(-1, (int)Utility::getParam('id', 0));
	
	if (empty($id) || $id < 0) {
		echo Constants::$ERR_INVALID_PARAMS['errmsg'];
		return ;
	}
	
	$indexModel = ObjectFinder::get('CustomerIndex');
	$customerInfo = $indexModel->getInfoById($id);
	
	if (empty($customerInfo)) {
		echo Constants::$ERR_INVALID_PARAMS['errmsg'];
		return ;
	}
	
	$childList = $indexModel->getList(array('pid'=>$id));
	if (!empty($childList['data'])) {
		$customerDetailID = $childList['data'][0]['customer_id'];
		$customerDetail = ObjectFinder::get('Customer')->getInfoById($customerDetailID);
		$customerDetail = $customerDetail[0];
	}

	include $GLOBALS['ROOT_PATH'] . '/views/admin/customer_modify.phtml';
}

function doModify() {
//	'id':detailID,
//	'idx':indexID,
//	'pid':indexPID,
//	'name':name,
//	'childName':childName,
//	'area':province+'_'+city+'_'+area,
//	'address':address,
//	'zipcode':zipcode,
//	'contact':contact,
//	'tel':tel,
//	'email':email

	$id = (int)Utility::getParam('id', 0);
	$idx = (int)Utility::getParam('index', 0);
	$pid = (int)Utility::getParam('pid', 0);
	$name = Utility::getParam('name', '');
	$childName = Utility::getParam('childName', '');
	$area = Utility::getParam('area', '');
	$address = Utility::getParam('address', '');
	$zipcode = Utility::getParam('zipcode', '');
	$contact = Utility::getParam('contact', '');
	$tel = Utility::getParam('tel', '');
	$email = Utility::getParam('email', '');
	
	if (empty($id) || empty($idx) || empty($pid) || empty($name) || empty($childName) || empty($area) 
		|| empty($address) || empty($zipcode) || empty($contact) || empty($tel)) {
		
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;
	}
	
	$customerDetail = ObjectFinder::get('Customer')->getInfoById($id);
	if (empty($customerDetail)) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	$customerDetail = $customerDetail[0];
	if ($customerDetail['customer_index'] != $idx || $customerDetail['customer_index_pid'] != $pid) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	
	$customerInfos = ObjectFinder::get('CustomerIndex')->getInfoByIds(array($idx, $pid));
	if (empty($customerInfos) || empty($customerInfos['data'])) {
		echoAjax(Constants::$ERR_INVALID_PARAMS);
		return ;
	}
	
	foreach($customerInfos['data'] as $customerInfo) {
		if ($customerInfo['id'] == $pid) {
			if ($customerInfo['name'] != $name) {
				ObjectFinder::get('CustomerIndex')->modify(array('name'=>$name), array('id'=>$pid));
			}
		}else if ($customerInfo['id'] == $idx) {
			if ($customerInfo['name'] != $childName) {
				ObjectFinder::get('CustomerIndex')->modify(array('name'=>$childName), array('id'=>$idx));
			}
		}
	}
	
	$data = array('name'=>"$name($childName)", 'area'=>$area, 'address'=>$address, 
	'zip_code'=>$zipcode, 'contact'=>$contact, 'tel'=>$tel, 'email'=>$email);
	
	ObjectFinder::get('Customer')->modify($data, array('id'=>$id));
	echoAjax(Constants::$ERR_SUCCESS);
}

function doDelete() {
	
	$pid = max(0, (int)Utility::getParam('id', 0));
	if (empty($pid)) {
		echoAjax(Constants::$ERR_MISSING_PARAMS);
		return ;	
	}
	
	$customerInfos = ObjectFinder::get('CustomerIndex')->getList(array('pid'=>$pid));
	if (!empty($customerInfos['data'])) {
		$childIDs = array();
		$customerIDs = array();
		foreach($customerInfos['data'] as $customerInfo) {
			$childIDs[] = $customerInfo['id'];
			$customerIDs[] = $customerInfo['customer_id'];
		}
		$res1 = ObjectFinder::get('Customer')->deleteByIds($customerIDs);
		$res2 = ObjectFinder::get('CustomerIndex')->deleteByIds($childIDs);
	}
	$res = ObjectFinder::get('CustomerIndex')->delete(array('id'=>$pid));
	echoAjax($res ? Constants::$ERR_SUCCESS : Constants::$ERR_FAILED);
}

function doDeleteChild() {
	
}

function manage() {
	
	$conf = $GLOBALS['conf'];
	$page = (int)Utility::getParam('page', 1);
    $start = (max($page, 1)-1)*$conf['pageSize'];
	$customerList = ObjectFinder::get('CustomerIndex')->getList(array('pid'=>0), $start, $conf['pageSize']);
	
	include $GLOBALS['ROOT_PATH'] . '/views/admin/customer_manage.phtml';
}
?>